package com.smmisia.article.controller;

import com.smmisia.article.es.Document;
import com.smmisia.article.es.DocumentMapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.easyes.core.kernel.EsWrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/es")
@Tag(name = "es模块", description = "es模块")
public class EsController {

    @Autowired
    private DocumentMapper documentMapper;


    @GetMapping("/createIndex")
    public void index(){
        boolean success = documentMapper.createIndex();
    }

    @GetMapping("/insert")
    public void insert(){
        // 测试插入数据
        Document document = new Document();
        document.setTitle("aaa");
        document.setContent("bbb");
        document.setStatus(0);
        document.setCreateTime(LocalDateTime.now().toString());
        int successCount = documentMapper.insert(document);
        System.out.println(successCount);
    }

    @GetMapping("/select")
    public void select(){
        // 测试查询 写法和MP一样 可以用链式,也可以非链式 根据使用习惯灵活选择即可
        String title = "aaa";
        Document document = EsWrappers.lambdaChainQuery(documentMapper)
                .eq(Document::getTitle, title)
                .one();
        System.out.println(document);
    }
}
